


//let Cell = require('./cell');





/**
 *
 * 基础摒除法
 */

function copyBoard(dst,src) {
    for(let i = 0; i < src.length;++i){
        for(let j = 0; j < src[0].length;++j){
            dst[i][j] = src[i][j];
        }
    }
}

// function markValue(tmpBoard,rows,cols,value) {
//     rows.forEach(row=>{
//         for(let i = 0; i < tmpBoard[0].length;++i){
//             tmpBoard[row][i] = -1;
//         }
//     });
//
//     cols.forEach(col=>{
//         for(let i = 0; i < tmpBoard.length;++i){
//             tmpBoard[i][col] = -1;
//         }
//     });
//
//     // 检查每一个宫,是否存在唯一空格
//
// }

let Methods = require('./methods');

function gongContainsValue(board,value,gongRowCount,gongColCount,startIds) {
    for(let i = 0; i < gongRowCount;++i){
        for(let j = 0; j < gongColCount; ++j){
            let ii = startIds.x + i;
            let jj = startIds.y + j;
            let num = board[ii][jj]; // 这里使用原来的  board
            if(num === value){
                return true;
            }
        }
    }
    return false;
}

function baseBC(boardInfo){
    let board = boardInfo.board;

    let tmpBoard = [];
    for(let i = 0; i < board.length;++i){
        tmpBoard[i] = [];
        for(let j = 0; j < board[0].length;++j){
            tmpBoard[i][j] = board[i][j];
        }
    }

    // 直接划掉 1 - 9 检查是否某一宫里面存在唯一解
    let gongCount = boardInfo.gongCount;
    let gongRowCount = boardInfo.gongRowCount;
    let gongColCount = boardInfo.gongColCount;

    for(let value = 1; value <= boardInfo.boardSize; ++value){

        copyBoard(tmpBoard,board);

        // 记录哪些row col 有 value
        let rows = [];
        let cols = [];
        for(let i = 0; i < board.length;++i){
            for(let j = 0; j < board[0].length;++j){
                let num = board[i][j];
                if(num === value){
                    rows.push(i);
                    cols.push(j);
                }
            }
        }

        // 都标记为 -1
        rows.forEach(row=>{
            for(let i = 0; i < tmpBoard[0].length;++i){
                tmpBoard[row][i] = -1;
            }
        });

        cols.forEach(col=>{
            for(let i = 0; i < tmpBoard.length;++i){
                tmpBoard[i][col] = -1;
            }
        });

        // 检查每一个宫,是否存在唯一空格
        for(let k = 0; k < gongCount; ++k){
            let startIds = boardInfo.getGongStartIdx(k);
            let zeroCount = 0;
            let zeroI = -1;
            let zeroJ = -1;
            for(let i = 0; i < gongRowCount;++i){
                for(let j = 0; j < gongColCount; ++j){
                    let ii = startIds.x + i;
                    let jj = startIds.y + j;
                    let num = tmpBoard[ii][jj];
                    if(num === 0){
                        zeroCount++;
                        zeroI = ii;
                        zeroJ = jj;
                    }
                }
            }

            if(zeroCount === 1){

                // 再判断所在的宫里面,是否包含 value
                if(gongContainsValue(board,value,gongRowCount,gongColCount,startIds)){
                    continue;
                }else{
                    // 找到唯一
                    return {
                        x:zeroI,
                        y:zeroJ,
                        value:value,
                        type:Methods.BaseBC
                    }
                }
            }
        }
    }
    return null;
}



let bTest = false;
if(bTest){
    let initBoard = [
        [7,0,0, 0,8,2, 0,4,0],
        [0,0,8, 0,6,3, 0,7,5],
        [0,9,0, 0,4,7, 2,0,8],

        [0,7,0, 8,5,0, 0,0,0],
        [8,0,9, 0,1,0, 5,0,7],
        [0,0,0, 0,7,9, 8,1,0],

        [0,0,7, 6,9,5, 3,8,0],
        [5,8,0, 7,3,1, 4,0,0],
        [9,3,6, 4,2,8, 7,5,1],
    ];

    let BorardInfo = require('./boardInfo');
    let boardInfo = new BorardInfo(initBoard);
    console.log(baseBC(boardInfo));
}


module.exports = baseBC;
